package com.higgins.eventtracking.client.health.fundamentals;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.DynamicTypeDTO;
import com.higgins.eventtracking.client.dto.GoalDTO;
import com.higgins.eventtracking.client.health.components.WeekNumberListBox;

public class GoalsSubTab extends VerticalPanel implements SubTab 		{
	private ScrollPanel achievementsScrollPanel = null;
	private final EventTrackingServiceAsync greetingService = GWT
														.create(EventTrackingService.class);
	private VerticalPanel panel = null;
	private GoalsColumns goalColumns = null;
	private GoalsTable goalTable = null;
	private boolean firstRun = true;
	private String parentType = null;
	private String frameOfMind = null;
//	private GoalsTab parentTab = null;
	private List<DynamicTypeDTO> subTypes = null;
	private List<String> subTypeNames = new ArrayList<String>();
	private TabPanel tabPanel = new TabPanel();
	private List<GoalsSubTab> subTabs = new ArrayList<GoalsSubTab>();
	
	public GoalsSubTab(String parentType, String frameOfMind, List<DynamicTypeDTO> subTypes)	{
		this.parentType = parentType;
		this.frameOfMind = frameOfMind;
		this.subTypes = subTypes;
	}
	public GoalsSubTab(String frameOfMind)	{
//		this.parentTab = parentTab;
		this.frameOfMind = frameOfMind;
		this.subTypes = new ArrayList<DynamicTypeDTO>();
	}
	public void update()	{
		if (firstRun)	{
			if (checkForSubItems())	{
				initialiseAsParentType();
			} else {
				initialiseAsSubType();
			}
		}
	}
	public Boolean checkForSubItems()	{
		Boolean subTypesExist = false;
		subTypeNames = new ArrayList<String>();
		for (DynamicTypeDTO dto:subTypes)	{
			if (dto.getCategory().equals(parentType) && dto.getDynamicType().equals(frameOfMind)
					&& dto.getSubType()!=null && dto.getSubType().equals(true) && dto.getSubCategory()!=null)	{
				subTypesExist = true;
				subTypeNames.add(dto.getSubCategory());
			}
		}
		return subTypesExist;
	}
	public void initialiseAsParentType()	{
		tabPanel.clear();
		addTabs(subTypeNames);
		add(tabPanel);
		if (subTabs.size()>0)	{
			subTabs.get(0).update();
		}
		tabPanel.addSelectionHandler(createSelectionHandler());
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
	        	GoalsSubTab selectedSubTab = subTabs.get(integerSelectionEvent.getSelectedItem());
	        	selectedSubTab.update();
	        }
		};
		return selectionHandler;
	}
	private void addTabs(List<String> frameOfMinds)	{
		subTabs = new ArrayList<GoalsSubTab>();
		for (String frameOfMind:frameOfMinds)	{
			GoalsSubTab subTab = new GoalsSubTab(frameOfMind);
			subTabs.add(subTab);
			tabPanel.add(subTab, frameOfMind);
		}
		tabPanel.selectTab(0);
	}

	public void initialiseAsSubType()	{
		clear();
		setHeight("20em");
//		populateGoals();
		goalColumns = new GoalsColumns();
		goalTable = new GoalsTable(goalColumns);
		goalTable.setFrameOfMind(frameOfMind);
		panel = new VerticalPanel();
		panel.add(goalTable);
		achievementsScrollPanel = new ScrollPanel();
		achievementsScrollPanel.add(panel);
		achievementsScrollPanel.setHeight("40em");
		add(achievementsScrollPanel);
//		firstRun = false;
	}
	

	private void populateGoals()	{
		greetingService.retrieveGoals(frameOfMind, new AsyncCallback<List<GoalDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
				//parentTab.setSubHeaderCount(frameOfMind, 0);
			}
			@Override
			public void onSuccess(List<GoalDTO> results) {
				addGoals(results);
				//parentTab.setSubHeaderCount(frameOfMind, results.size());
			}
		});		
	}
	
	private void addGoals(List<GoalDTO> results)	{
		try	{
			achievementsScrollPanel.remove(panel);
		} catch (Exception e)	{
			
		}
		panel = new VerticalPanel();
		for (GoalDTO result:results)	{
			Label label = new Label(result.getTask());
			panel.add(label);
			panel.setCellHeight(label, Integer.toString(label.getElement().getOffsetHeight())+"px");
		}
		panel.setHeight(String.valueOf(results.size())+ "em");
		achievementsScrollPanel.add(panel);
		achievementsScrollPanel.setHeight("15em");

	}
}
